package graphicinterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.AdministratorOfAllBookTagsUsedByTheSystem;

public class WindowChooseBookTag implements MouseListener
{
	/**
	 * book tag selected by user in this window.
	 */
	private String selectedBooktag;
	/**
	 * list containing  all book tags which can be shown in window.
	 */
	private LinkedList<String> bookTags;
	/**
	 * list containing respective panels for the books tags. All these panel will be selectable.
	 */
	private LinkedList<JPanel> panelsForBookTags;
	
	/**
	 * //how many tags to show per page in this window?
	 */
	private int numberOfTagsPerPage;
	
	/**
	 * what's the name of the window that comes right after user selects the book tag?
	 */
	private String nameWindowComesAfter;
	private JFrame windowSelectTag;
	
	public WindowChooseBookTag(String whichWindowComesAfter)
	{
		this.selectedBooktag = "none";//no book tags were selected yet.
		this.numberOfTagsPerPage = 5;
		this.nameWindowComesAfter = whichWindowComesAfter;
		this.bookTags = 
				AdministratorOfAllBookTagsUsedByTheSystem.getAllBookTags();
		//let's create panels for the book tags!
		this.panelsForBookTags = new LinkedList<JPanel>();
		for(int i = 0 ; i < bookTags.size(); i++)
		{
			String singleTag = bookTags.get(i);
			JPanel panelForSingleBooktag = new JPanel();
			panelForSingleBooktag.setBackground(new Color(255, 131, 137));
			//how to add a String tag to a panel?through a label!
			JLabel labelForSingleTag = TextLabelCreator.createStyledTextlabel(singleTag);
			panelForSingleBooktag.add(labelForSingleTag);
			//this panel can be selected
			panelForSingleBooktag.addMouseListener(this);
			this.panelsForBookTags.add(panelForSingleBooktag);
		}
		
		windowSelectTag = new JFrameWithMenu("choose book tag");
		
		this.drawTagSelectionWindow();
	}
	
	public String getSelectedTag()
	{
		return this.selectedBooktag;
	}
	
	
	
	
	private JPanel createpanelWindowTitle()
	{
		JPanel windowTitle = new JPanel();
		windowTitle.setBackground(new Color(255, 131, 137));
		windowTitle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
		JLabel labelTitle = TextLabelCreator.createStyledTextlabel("Choose a book tag", 30);
		windowTitle.add(labelTitle);
		return windowTitle;
	}
	
	/**
	 * creates a panel with a character that will appear on page's side
	 * @return panel with a character
	 */
	private JPanel createPanelMascotCharacter()
	{
		JPanel panelMascotCharacter = new JPanel();
		panelMascotCharacter.setBackground(new Color(255, 131, 137));
		ImageIcon imageMascot = CreatesImageIcon.createImageIcon("images/maidchan.png", "maid mascot");
		JLabel labelWithMascotCharacter = new JLabel(imageMascot);
		panelMascotCharacter.add(labelWithMascotCharacter);
		return panelMascotCharacter;
	}
	
	private JPanel createPanelWithTags()
	{
		/*
		 * not all book tags will be shown at the same time: there will be a limit per page
		 * and the user will transit pages looking for the tag. Then, let's use the 
		 * panel structure we created to show components by page.
		 */
		ShowsJPanelsOnManyPages panelShowsBookTagsByPage = 
				new ShowsJPanelsOnManyPages(this.panelsForBookTags, this.numberOfTagsPerPage, this.numberOfTagsPerPage);
		
		return panelShowsBookTagsByPage;
	}
	
	private void drawTagSelectionWindow()
	{
		//first, we'll create the window's components
		JPanel panelWindowTitle = this.createpanelWindowTitle();
		JPanel panelMascot = this.createPanelMascotCharacter();
		JPanel panelBookTagsToClick = this.createPanelWithTags();
		//now, let's add them in one single panel!
		JPanelWithBackgroundImage panelTagSelection = new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		GridBagLayout layoutPaneltagSelection = new GridBagLayout();
		panelTagSelection.setLayout(layoutPaneltagSelection);
		GridBagConstraints g = new GridBagConstraints();
		g.gridx = 0;
		g.gridy = 0;
		g.insets = new Insets(0, 0, 10, 0);
		panelTagSelection.add(panelWindowTitle, g);
		g.gridy = 1;
		panelTagSelection.add(panelMascot, g);
		g.gridx = 1;
		panelTagSelection.add(panelBookTagsToClick, g);
		
		this.windowSelectTag.add(panelTagSelection);
		this.windowSelectTag.setVisible(true);
	}
	
	

	
	
	@Override
	public void mouseClicked(MouseEvent e) 
	{
		if(userSelectedATag(e) == true)
		{
			//user just selected a tag
			this.selectedBooktag = this.getTagSelectedByUser(e);
			
			/*let's change the cursor to a loading 
			  cursor just until the new window has finished drawing*/
			Component objectClicked = e.getComponent();
			objectClicked.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			
			if(this.nameWindowComesAfter.compareTo("InterfaceBestAuthorsBasedOnTag") == 0)
			{
				WindowBestAuthorsBasedOnTag windowBestAuthors = 
						new WindowBestAuthorsBasedOnTag(selectedBooktag);
			}
			else if(this.nameWindowComesAfter.compareTo("InterfaceBooksFromTag") == 0)
			{
				WindowBestBooksFromTag windowBestBooksFromTag = 
						new WindowBestBooksFromTag(selectedBooktag);
			}
			else if(this.nameWindowComesAfter.compareTo("InterfaceRecentReviewsByTag") == 0)
			{
				WindowRecentReviewsByTag windowRecentBooksByTag = 
						new WindowRecentReviewsByTag(selectedBooktag);
			}
			
			//let's also dispose the window
			this.windowSelectTag.dispose();
		}
		
	}
	
	/**
	 * given a click mouse event, it checks if the source of this event is
	 * a selectable tag from the window.
	 * @param e click mouse event
	 * @return boolean representing if user clicked on tag.
	 */
	private boolean userSelectedATag(MouseEvent e)
	{
		Object objectSelected = e.getSource();
		for(int i = 0; i < this.panelsForBookTags.size(); i++)
		{
			JPanel singlePanelSelectableBookTag = this.panelsForBookTags.get(i);
			if(objectSelected == singlePanelSelectableBookTag)
			{
				return true;
			}
		}
		
		return false;
	}
	
	private String getTagSelectedByUser(MouseEvent e)
	{
		Object objectSelected = e.getSource();
		for(int i = 0; i < this.panelsForBookTags.size(); i++)
		{
			JPanel singlePanelSelectableBookTag = this.panelsForBookTags.get(i);
			if(objectSelected == singlePanelSelectableBookTag)
			{
				//user selected this panel which is associated with a tag in the bookTags list
				String tagAssociatedWithpanel = this.bookTags.get(i);
				return tagAssociatedWithpanel;
			}
		}
		
		return "none";//click was not for a stored tag...
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
